//
//  WoDeTableViewController.m
//  NC-Trendy
//
//  Created by ljd on 16/2/24.
//  Copyright © 2016年 liang. All rights reserved.
//

#import "WoDeTableViewController.h"

#import "DengLuViewController.h"

#import "UserModel.h"

#import "ZhuCeViewController.h"

#import "XiuGaiXinXiViewController.h"
@interface WoDeTableViewController ()

@property(nonatomic,strong)UserModel *userModel;
@property (weak, nonatomic) IBOutlet UIImageView *userImageVIew;


@end

@implementation WoDeTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
//页面将要显示的时候执行
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    //将要显示页面的时候，将模型参数传过来
    AppDelegate *app = [AppDelegate shareAppDelegate];
    if (app.userModel) {
        self.userNameLabel.text = app.userModel.user_name;
        self.userEmailLabel.text = app.userModel.user_email;
        //self.userImageVIew.image = [NSString stringWith];
    }
    [self.tableView reloadData];
}
//选中行事件
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(nonnull NSIndexPath *)indexPath{
    AppDelegate *appdelegate = [AppDelegate shareAppDelegate];
    //如果选中第一组(用户栏)
    if (indexPath.section == 0) {
        //如果已经是登录状态，跳转到修改用户信息控制器
        if ([self.userNameLabel.text isEqualToString:appdelegate.userModel.user_name] &&[self.userEmailLabel.text isEqualToString:appdelegate.userModel.user_email]) {
            XiuGaiXinXiViewController *XGVC = [[XiuGaiXinXiViewController alloc]init];
            [self.navigationController pushViewController:XGVC animated:YES];
        }else{

        //如果是未登陆状态,跳转到登录VC
        DengLuViewController *dengLuVC = [[DengLuViewController alloc]init];
        [self.navigationController pushViewController:dengLuVC animated:YES];
        }
    }else if (indexPath.section == 3){
        //清除缓存
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil
                                                            message:@"是否清理缓存？"
                                                           delegate:self
                                                  cancelButtonTitle:@"取消"
                                                  otherButtonTitles:@"确定", nil];
        alertView.tag = 1011;
        [alertView show];
    }
}

#pragma mark 通过找到对应的alertView，然后来实现对应的功能
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 1 && alertView.tag == 1011) {
        [self clearCache];
        
    }
}

#pragma mark 清理缓存，开启异步任务
-(void) clearCache
{
    dispatch_async(
                   dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
                   , ^{
                       //找到本地的caches目录
                       NSString *cachPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0];
                       //查找给定路径下的所有子路径。深度查找，不限于当前层，并且放在数组里
                       NSArray *files = [[NSFileManager defaultManager] subpathsAtPath:cachPath];
                       //对所有的子路径的数组进行遍历
                       for (NSString *p in files) {
                           NSError *error;
                           //向现有路径添加一个component。斜杠/会被自动加上
                           NSString *path = [cachPath stringByAppendingPathComponent:p];
                           //判断文件是否位于path路径下面,如果在就删除
                           if ([[NSFileManager defaultManager] fileExistsAtPath:path]) {
                               [[NSFileManager defaultManager] removeItemAtPath:path error:&error];
                           }
                       }
                       //返回主线程显示清除缓存成功
                       [self performSelectorOnMainThread:@selector(clearCacheSuccess)
                                              withObject:nil waitUntilDone:YES];});
}

#pragma mark 清除缓存成功弹窗
-(void)clearCacheSuccess
{
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil
                                                        message:@"缓存清理成功！"
                                                       delegate:nil
                                              cancelButtonTitle:@"确定"
                                              otherButtonTitles:nil, nil];
    [alertView show];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

//- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
//#warning Incomplete implementation, return the number of sections
//    return 5;
//}
//
//- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
//#warning Incomplete implementation, return the number of rows
//    switch (section) {
//        case 1:
//            return 2;
//            break;
//        default:
//            return 1;
//            break;
//    }
//}
/*
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell1 = [tableView dequeueReusableCellWithIdentifier:@"reuseIdentifier" forIndexPath:indexPath];
    UITableViewCell *cell2 = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    // Configure the cell...
    return cell1;
}
*/
/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
